package com.sunrise.relatorios.bean;

import java.util.ArrayList;
import java.util.Date;

import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import lombok.Getter;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.sunrise.generico.entidade.Conta;
import com.sunrise.generico.entidade.Usuario;
import com.sunrise.relatorios.servico.GerarRelatorioService;
import com.sunrise.relatorios.visao.GerarRelatorioForm;


@ManagedBean
@ViewScoped
@Controller
public class GerarRelatorio {

	@Resource
	@Getter
	private GerarRelatorioService gerarRelatorioService;

	@Resource
	@Getter
	private GerarRelatorioForm formulario;

	public Usuario obterUsuarioLogado() {
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		return getGerarRelatorioService().buscarUsuarioPorUsername(auth.getName());
	}

	private void limparFormulario() {
		getFormulario().setContas(new ArrayList<Conta>());
		getFormulario().setIdConta(null);
		getFormulario().setDataFim(null);
		getFormulario().setDataInicio(null);
		getFormulario().setFluxoCaixa(Boolean.FALSE);
		getFormulario().setEvolucaoSaldo(Boolean.FALSE);
		getFormulario().setExtratoConta(Boolean.FALSE);

	}

	public void fluxoCaixa() {
		limparFormulario();
		getFormulario().setContas(getGerarRelatorioService().listarContasPorUsuario(obterUsuarioLogado()));
		getFormulario().setFluxoCaixa(Boolean.TRUE);
	}

	public void evolucaoSaldo() {
		limparFormulario();
		getFormulario().setEvolucaoSaldo(Boolean.TRUE);
	}

	public void extratoConta() {
		limparFormulario();
		getFormulario().setExtratoConta(Boolean.TRUE);
	}

	public String gerarFluxoCaixa() {
		 getGerarRelatorioService().gerarRelatorioFluxoCaixa(getFormulario());
		 getFormulario().setUsuarioLogado(obterUsuarioLogado());
		 getFormulario().setDataEmissao(new Date());
		 return "fluxoCaixa.xhtml?faces-redirect=true&amp;includeViewParams=true";
		
	}

}
